package com.example.lastwork2.register;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.SystemClock;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;


import com.example.lastwork2.MainActivity;
import com.example.lastwork2.R;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class RegisterActivity extends AppCompatActivity {

    private EditText edusername;
    private EditText edpwd1;
    private EditText edpwd2;
    private Button btnRegister;

    private Handler handler;
    private long lastonclickTime = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        //绑定控件
        getViews();



        //点击注册
        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(System.currentTimeMillis()-lastonclickTime<=1000){
                    return;
                }else{
                    lastonclickTime = System.currentTimeMillis();
                    //获取输入框中的内容
                    String username = edusername.getText().toString();
                    String pwd1 = edpwd1.getText().toString();
                    String pwd2 = edpwd2.getText().toString();
                    //1.判断是否为空
                    if (username.equals("") || pwd1.equals("") || pwd2.equals("")){
                        //弹窗警告  用户名或密码不能为空
                        //弹窗用户名或密码错误
                        new AlertDialog.Builder(RegisterActivity.this)
                                .setTitle("注册失败")
                                .setMessage("用户名或密码不能为空")
                                .show();
                    }else if(!pwd1.equals(pwd2)){
                        new AlertDialog.Builder(RegisterActivity.this)
                                .setTitle("注册失败")
                                .setMessage("两次输入密码不一致")
                                .show();
                    }else{
                        //向服务器传数据
                        sendToServer(username,pwd1);
                        handler = new Handler(Looper.getMainLooper()){
                            @Override
                            public void handleMessage(@NonNull Message msg) {
                                switch (msg.what){
                                    case -1:
                                        //弹窗警告注册失败
                                        new AlertDialog.Builder(RegisterActivity.this)
                                                .setTitle("注册失败")
                                                .show();
                                        break;
                                    case 1:
                                        //弹窗注册成功
                                        new AlertDialog.Builder(RegisterActivity.this)
                                                .setTitle("注册成功")
                                                .setMessage("请返回重新登录")
                                                .show();
                                        break;
                                    case 2:
                                        //弹窗该用户已存在
                                        new AlertDialog.Builder(RegisterActivity.this)
                                                .setTitle("注册失败")
                                                .setMessage("该用户已存在")
                                                .show();
                                        break;
                                }
                            }
                        };
                    }
                }





            }
        });




    }

    private void sendToServer(String username, String pwd1)  {

        String urlPath = "http://10.0.2.2:8080/bigwork/register?username="+username+"&password="+pwd1;

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    URL url = new URL(urlPath);
                    URLConnection urlConnection = url.openConnection();

                    //接收服务器传来的数据
                    InputStream inputStream = urlConnection.getInputStream();
                    //创建字符流读取数据
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"utf-8"));
                    String str = bufferedReader.readLine();
                    //服务器发来的数据
                    System.out.println(str); //成功收到1  不成功收到-1
                    //通过Message+handler发送到主线程
                    if ("1".equals(str)){
                        Message msg = handler.obtainMessage();
                        msg.what = 1;
                        handler.sendMessage(msg);
                    }else if ("2".equals(str)){
                        Message msg = handler.obtainMessage();
                        msg.what = 2;
                        handler.sendMessage(msg);
                    }else
                    {
                        Message msg = handler.obtainMessage();
                        msg.what = -1;
                        handler.sendMessage(msg);
                    }






                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }


//
            }
        }).start();


    }


    //绑定控件
    private void getViews() {
        edusername = findViewById(R.id.user_name);
        edpwd1 = findViewById(R.id.user_password1);
        edpwd2 = findViewById(R.id.user_password2);
        btnRegister = findViewById(R.id.register);

    }
}